//
//  JFBaseAlertView.m
//  弹窗哈哈哈
//
//  Created by Alan Wang on 2018/11/8.
//  Copyright © 2018 kuaijiankang. All rights reserved.
//

#import "JFBaseAlertView.h"
#import "JFKitPri.h"

//#define AWAlertScreenWidth ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
//
//#define AWAlertScreenHeight ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)

#define AWAlertScreenWidth [UIScreen mainScreen].bounds.size.width
#define AWAlertScreenHeight [UIScreen mainScreen].bounds.size.height

///各个栏目之间的距离
//#define AWAlertSpace 15.0
#define AWAlertSpace 30.0
#define AWAlertSpace4 5.0
#define AWAlertSpace2 30.0
#define AWAlertSpace3 26.0
//系统样式的默认宽度 53
#define AWAlertMargin  30
#define AWAlertCusMargin  30
#define AWAlertContontWithMarginWidth (AWAlertScreenWidth-AWAlertMargin*2)
#define AWAlertCusContontWithMarginWidth (AWAlertScreenWidth-AWAlertCusMargin*2)
#define AWAlertContontWidth (AWAlertScreenWidth-AWAlertMargin*2-2*AWAlertSpace)

#define AWAlertTitleHeight 17.0
#define AWAlertBtnHeight 44
#define AWCustomAlertBtnHeight 40
#define AWAlertLineWidth 0.5
#define AWAlertWindowsTag 88888



@interface JFBaseAlertView()

@property(nonatomic,strong) UIWindow * rootWindow;

@end
@implementation JFBaseAlertView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.clickBackGroundHide = YES;
        self.titleColor = [UIColor awColorWithHexString:@"#333333"];
        self.messageColor = [UIColor awColorWithHexString:@"#666666"];
        self.subtitleColor = [UIColor awColorWithHexString:@"#666666"];
        self.messageMaxHeight = 14*10;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}
#pragma mark--辅助方法
-(void)initUI{
    self.frame = [UIScreen mainScreen].bounds;
    [self addSubview:self.shodowView];
    self.shodowView.alpha = 0;
    [self.shodowView addSubview:self.contentView];
    if(self.style == JFAlertViewStyleAlert){
        [self  initAlertStyle];
    }else if (self.style == JFAlertViewStyleActionSheet){
        [self initActionSheet];
    }else if (self.style == JFAlertViewStyleCustomActionSheet){
        [self initCustomActionSheet];
    }else if (self.style == JFAlertViewStyleCustomViewOnly){
          [self initCustomViewOnly];
    }
}
-(void)setBlur:(BOOL)blur{
    _blur = blur;
    UIView * view = [self.shodowView viewWithTag:9999];
    if (view) {
        [view removeFromSuperview];
    }
    if(blur){
        UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        UIVisualEffectView * effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.alpha = 0.9;
        effectview.frame = self.shodowView.bounds;
        effectview.tag =9999;
        [self.shodowView insertSubview:effectview atIndex:0];
    }
}
-(void)initActionSheet{
    CGFloat y =15;
    if(self.title){
        y+=AWAlertSpace4;
        UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
            lab.AWtext(self.title).AWx(0).AWy(y).AWwidth(AWAlertScreenWidth).AWheight(AWAlertTitleHeight).AWfont([UIFont systemFontOfSize:16.0f]).AWtextAlignment(NSTextAlignmentCenter).AWtextColor([UIColor awColorWithHexString:@"#333333"]).AWbackgroundColor(HexColor(@"#ffffff"));
        }];
        if (self.titleColor) {
            tempLab.textColor = self.titleColor;
        }
        [self.contentView addSubview:tempLab];
        y+=AWAlertTitleHeight;
    }
    if(self.message){
        y+=AWAlertSpace4;
        CGFloat height = [JFStringHelper getHeightLineWithString:self.message lineSpacing:2 withWidth:AWAlertScreenWidth withFont:[UIFont systemFontOfSize:16]];
        UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
            lab.AWtext(self.message).AWx(0).AWy(y).AWwidth(AWAlertScreenWidth).AWheight(height).AWfont([UIFont systemFontOfSize:15.0f]).AWtextAlignment(NSTextAlignmentCenter).AWtextColor([UIColor awColorWithHexString:@"#666666"]).AWbackgroundColor(HexColor(@"#ffffff")).AWlineBreakMode(NSLineBreakByWordWrapping);
            lab.numberOfLines =0;
        }];
        if (self.subtitleColor) {
            tempLab.textColor = self.subtitleColor;
        }
        
        [self.contentView addSubview:tempLab];
        y+=height;
    }
    
    
    NSInteger count = self.actionArr.count;
    if(count == 0){//扩展的系统样式
        JFAlertAction * action = [JFAlertAction actionWithTitle:@"取消" titleColor:[UIColor redColor] titleFont:nil ext:nil actionBlock:^(id data) {
        }];
        [self.actionArr addObject:action];
    }
    
    CGFloat  width = AWAlertScreenWidth;
    for (int i=0; i<count-1; i++) {
        JFAlertAction * action = self.actionArr[i];
        UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
            btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(i).AWclickAction(self, @selector(btnClick:)).AWbackgroundColor(HexColor(@"#ffffff"));
        }];
        [self.contentView addSubview:btn];
        y+=AWAlertBtnHeight;
        if(self.showSplitLine){
            if(i != (count-2)){
                UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
                    lab.AWx(0).AWy(y).AWwidth(AWAlertScreenWidth).AWheight(AWAlertLineWidth).AWbackgroundColor([UIColor colorWithHexString:@"#EFF0F0"]);
                }];
                [self.contentView addSubview:tempLab];
                y+=AWAlertLineWidth;
            }
        }
    }
    NSInteger index = count -1;
    if(index>=0){
        
        UIView * view = [UIView AW_View:^(UIView *view) {
            view.AWbackgroundColor([UIColor colorWithHexString:@"#F5F5F5"]).AWframeQ(0, y, AWAlertScreenWidth, AWAlertSpace4);
        }];
        [self.contentView addSubview:view];
        y+=AWAlertSpace4;
        
        JFAlertAction * action = self.actionArr[index];
        UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
            btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(index).AWclickAction(self, @selector(btnClick:)).AWbackgroundColor(HexColor(@"#ffffff"));
        }];
        [self.contentView addSubview:btn];
         y+=AWAlertBtnHeight;
    }
    if([JFDeviceInfo isFullScreen]){//底部的偏移量
        y+=34;
    }
//    self.contentView.backgroundColor =  AWThemeColorForKey(@"MeGlobal.backgroundColor2");
    self.contentView.AWheight(y).AWwidth(AWAlertScreenWidth);
    self.contentView.AWx(0).AWy(AWAlertScreenHeight);
}
-(void)initCustomViewOnly{
    if (self.customcontentView) {
        self.customcontentView.AWx(0).AWy(0);
        [self.contentView addSubview:self.customcontentView];
        self.contentView.AWcornerRadius(4);
        self.contentView.AWheight(self.customcontentView.height).AWwidth(self.customcontentView.width);
        self.contentView.center = self.center;
    }
}
-(void)initCustomActionSheet{
    CGFloat y =0;

    NSInteger count = self.actionArr.count;
    if(count == 0){//扩展的系统样式
        return;
    }
    CGFloat  width = AWAlertScreenWidth;
    for (int i=0; i<count-1; i++) {
        JFAlertAction * action = self.actionArr[i];
        UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
            btn.AWframe(CGRectMake(0, y, width, AWCustomAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(i).AWclickAction(self, @selector(btnClick:)).AWbackgroundColor(AWThemeColorForKey(@"MeGlobal.backgroundColor2"));
        }];
        [self.contentView addSubview:btn];
        y+=AWCustomAlertBtnHeight;
        if(self.showSplitLine){
            if(i != (count-2)){
                UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
                    lab.AWx(0).AWy(y).AWwidth(AWAlertScreenWidth).AWheight(AWAlertLineWidth).AWbackgroundColor(AWThemeColorForKey(@"MeGlobal.backgroundColor"));
                }];
                [self.contentView addSubview:tempLab];
                y+=AWAlertLineWidth;
            }
        }
    }
    NSInteger index = count -1;
    if(index>=0){
        
        UIView * view = [UIView AW_View:^(UIView *view) {
            view.AWbackgroundColor(AWThemeColorForKey(@"MeGlobal.backgroundColor")).AWframeQ(0, y, AWAlertScreenWidth, AWAlertLineWidth);
        }];
        [self.contentView addSubview:view];
        y+=AWAlertLineWidth;
        
        JFAlertAction * action = self.actionArr[index];
        UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
            btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(index).AWclickAction(self, @selector(btnClick:)).AWbackgroundColor(AWThemeColorForKey(@"MeGlobal.backgroundColor2"));
        }];
        [self.contentView addSubview:btn];
         y+=AWAlertBtnHeight;
    }
    if([JFDeviceInfo isFullScreen]){//底部的偏移量
        y+=34;
    }
    self.contentView.backgroundColor =  AWThemeColorForKey(@"MeGlobal.backgroundColor2");
    self.contentView.AWheight(y).AWwidth(AWAlertScreenWidth);
    self.contentView.AWx(0).AWy(AWAlertScreenHeight);
}
-(void)initAlertStyle{
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 5.f;
    CGFloat y =0;
    if(self.title){
        y+=AWAlertSpace4+15;
        UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
            lab.AWtext(self.title).AWx(AWAlertSpace).AWy(y).AWwidth(AWAlertContontWidth).AWheight(AWAlertTitleHeight).AWfont([UIFont systemFontOfSize:16.0f]).AWtextAlignment(NSTextAlignmentCenter).AWtextColor([UIColor awColorWithHexString:@"#333333"]);
        }];
        if (self.titleColor) {
            tempLab.textColor = self.titleColor;
        }
        [self.contentView addSubview:tempLab];
        y+=AWAlertTitleHeight;
    }
    if(self.message){
        y+=AWAlertSpace2;
        if(self.title){
            y-=10;
        }
        CGFloat height = [JFStringHelper getHeightLineWithString:self.message lineSpacing:0 withWidth:AWAlertContontWidth withFont:[UIFont systemFontOfSize:14]];
        
        if (height > self.messageMaxHeight) {//超过10行
            height = self.messageMaxHeight;
            UITextView * tempLab = [UITextView AW_TextView:^(UITextView *view) {
             view.AWtext(self.message).AWx(AWAlertSpace2).AWy(y).AWwidth(AWAlertContontWidth).AWheight(height).AWfont([UIFont systemFontOfSize:14.0f]).AWtextAlignment(NSTextAlignmentCenter).AWtextColor([UIColor awColorWithHexString:@"#666666"]);
                
            }];
            tempLab.editable = NO;
            if (self.messageTextAlignment != 1) {
                tempLab.AWtextAlignment(self.messageTextAlignment);
            }
            if (self.messageColor) {
                tempLab.textColor = self.messageColor;
            }
            [self.contentView addSubview:tempLab];
            y+=height;

        }else{
            UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
            lab.AWtext(self.message).AWx(AWAlertSpace2).AWy(y).AWwidth(AWAlertContontWidth).AWheight(height).AWfont([UIFont systemFontOfSize:14.0f]).AWtextAlignment(NSTextAlignmentCenter).AWtextColor([UIColor awColorWithHexString:@"#666666"]).AWlineBreakMode(NSLineBreakByWordWrapping);
                lab.numberOfLines =0;
            }];
            if (self.messageTextAlignment != 1) {
                tempLab.AWtextAlignment(self.messageTextAlignment);
            }
            if (self.messageColor) {
                tempLab.textColor = self.messageColor;
            }
            [self.contentView addSubview:tempLab];
            y+=height;
        }

    }
    
    if(self.isCustom && self.customcontentView){//如果有自己的视图的话
         y+=AWAlertSpace4;
        CGSize size = self.customcontentView.frame.size;
        if (size.width==0) {
            size.width = AWAlertContontWithMarginWidth;
        }
        
        if (size.width > AWAlertContontWithMarginWidth) {
            size.width = AWAlertContontWithMarginWidth;
        }
        CGFloat x = (size.width >= AWAlertContontWithMarginWidth) ? 0 : ((AWAlertContontWithMarginWidth-size.width)/2);
        self.customcontentView.frame = CGRectMake(x, y, size.width, size.height);
        [self.contentView addSubview:self.customcontentView];
        y += size.height;
    }
    
    NSInteger count = self.actionArr.count;
    if(count){//扩展的系统样式
        if(self.btnOffset > 0){
                y+=self.btnOffset;
        }else{
               y+=AWAlertSpace3;
        }
   
        UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
            lab.AWx(0).AWy(y).AWwidth(AWAlertContontWithMarginWidth).AWheight(AWAlertLineWidth).AWbackgroundColor([UIColor awColorWithHexString:@"#E5E5E2"]);
        }];
        [self.contentView addSubview:tempLab];
        y+=AWAlertLineWidth;
        CGFloat width = (AWAlertContontWithMarginWidth - (count -1)*0.5)/count;
        
        if(count ==1){
            JFAlertAction * action = self.actionArr[0];
            UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
                btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(0).AWclickAction(self, @selector(btnClick:));
                if (action.tag >0) {
                    btn.tag = action.tag;
                }
                btn.enabled = action.enable;
            }];
            [self.contentView addSubview:btn];
            y+=AWAlertBtnHeight;
            
        }else if(count == 2){
            JFAlertAction * action = self.actionArr[0];
            UIButton * btn1 = [UIButton AW_Button:^(UIButton *btn) {
                btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(0).AWclickAction(self, @selector(btnClick:));
                if (action.tag >0) {
                    btn.tag = action.tag;
                }
                btn.enabled = action.enable;
            }];
            [self.contentView addSubview:btn1];
            
            UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
                lab.AWx(CGRectGetMaxX(btn1.frame)).AWy(y).AWwidth(AWAlertLineWidth).AWheight(AWAlertBtnHeight).AWbackgroundColor([UIColor awColorWithHexString:@"#E5E5E2"]);
            }];
            [self.contentView addSubview:tempLab];
            
            JFAlertAction * action2 = self.actionArr[1];
            UIButton * btn2 = [UIButton AW_Button:^(UIButton *btn) {
                btn.AWframe(CGRectMake(CGRectGetMaxX(tempLab.frame), y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action2.titleFont).AWtitleNormalState(action2.title).AWtitleColor(action2.titleColor, UIControlStateNormal).AWtag(1).AWclickAction(self, @selector(btnClick:));
                
                if (action2.tag >0) {
                    btn.tag = action2.tag;
                }
                btn.enabled = action2.enable;
            }];
            [self.contentView addSubview:btn2];
            
            
            y+=AWAlertBtnHeight;
            
            
        }else{
            width = AWAlertContontWithMarginWidth;
            for (int i=0; i<count; i++) {
                
                JFAlertAction * action = self.actionArr[i];
                UIButton * btn = [UIButton AW_Button:^(UIButton *btn) {
                    btn.AWframe(CGRectMake(0, y, width, AWAlertBtnHeight)).AWbackgroundColor([UIColor clearColor]).AWtitlefont(action.titleFont).AWtitleNormalState(action.title).AWtitleColor(action.titleColor, UIControlStateNormal).AWtag(i).AWclickAction(self, @selector(btnClick:));
                    if (action.tag >0) {
                        btn.tag = action.tag;
                    }
                    btn.enabled = action.enable;
                }];
                [self.contentView addSubview:btn];
                y+=AWAlertBtnHeight;
                
                if(i != (count-1)){
                    UILabel * tempLab = [UILabel AW_Lable:^(UILabel *lab) {
                        lab.AWx(0).AWy(y).AWwidth(AWAlertContontWithMarginWidth).AWheight(AWAlertLineWidth).AWbackgroundColor([UIColor awColorWithHexString:@"#E5E5E2"]);
                    }];
                    [self.contentView addSubview:tempLab];
                    y+=AWAlertLineWidth;
                }
                
            }
        }
        
    }
    self.contentView.AWheight(y).AWwidth(AWAlertContontWithMarginWidth);
    self.contentView.center = self.center;
}

#pragma mark--事件控制

-(void)btnClick:(UIButton*)btn{
    if (!self.closeAutoHide) {
        [self hide];
    }
    NSInteger index = btn.tag;
    for (JFAlertAction * action in self.actionArr){
        if (action.tag ==  index) {
            if(action.actionBlock){
                if (self.isCustom) {
                    action.actionBlock(self);
                }else{
                    action.actionBlock(action.ext);
                }
            }
            break;
        }
    }
        
//        JFAlertAction * action = self.actionArr[index];
//        if(action.actionBlock){
//            if (self.isCustom) {
//                action.actionBlock(self);
//            }else{
//                action.actionBlock(action.ext);
//            }
//        }
}
-(void)show{
    self.rootWindow.hidden = NO;
    
    NSArray *  arr = [self.rootWindow subviews];
    for (UIView * view in arr) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            [view removeFromSuperview];
        } completion:^(BOOL finished) {
            
        }];
        
    }
    [self initUI];
    [self.rootWindow addSubview:self];
    
    
    
    CGRect oldRect = self.contentView.frame;
    if (!(self.style == JFAlertViewStyleActionSheet || self.style == JFAlertViewStyleCustomActionSheet)) {
        CGRect newRect = oldRect;
        newRect.size.height = 0;
        newRect.size.width = 0;
        newRect.origin.x = AWSCREEN_WIDTH / 2;
        newRect.origin.y =  AWSCREENH_HEIGHT / 2;
        self.contentView.frame = newRect;
        self.contentView.alpha = 0;
    }
    [self.rootWindow setNeedsLayout];
    [self.rootWindow setNeedsDisplay];
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews animations:^{
         self.shodowView.alpha = 1;
          self.contentView.alpha = 1;

        if(self.style == JFAlertViewStyleActionSheet || self.style == JFAlertViewStyleCustomActionSheet){
            CGRect frame = self.contentView.frame;
            frame.origin.y -= frame.size.height;
            [self.contentView setFrame:frame];
        }else {
            self.contentView.frame = oldRect;
        }

    } completion:nil];
}
-(void)showWithAnimation:(JFAlertViewAnimation)animationType{
    self.rootWindow.hidden = NO;
    
    NSArray *  arr = [self.rootWindow subviews];
    for (UIView * view in arr) {
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            [view removeFromSuperview];
        } completion:^(BOOL finished) {
            
        }];
    }
    [self initUI];
    [self.rootWindow addSubview:self];
    
    
    self.shodowView.alpha = 1;
    switch (animationType) {
        case JFAlertViewAnimationNone:{
            
        }break;
        case JFAlertViewAnimationPullDown:{
            
        }break;
        default:
            break;
    }
    
    

}

-(void)showFromView:(UIView*)view{
   
    [self initUI];
    [view addSubview:self];
    [view bringSubviewToFront:self];
    

    
    CGRect oldRect = self.contentView.frame;
    if (!(self.style == JFAlertViewStyleActionSheet || self.style == JFAlertViewStyleCustomActionSheet)) {
        CGRect newRect = oldRect;
        newRect.size.height = 0;
        newRect.size.width = 0;
        newRect.origin.x = AWSCREEN_WIDTH / 2;
        newRect.origin.y =  AWSCREENH_HEIGHT / 2;
        self.contentView.frame = newRect;
        self.contentView.alpha = 0;
    }

    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews animations:^{
        self.shodowView.alpha = 1;
        self.contentView.alpha = 1;

        if(self.style == JFAlertViewStyleActionSheet){
            CGRect frame = self.contentView.frame;
            frame.origin.y -= frame.size.height;
            [self.contentView setFrame:frame];
        }else {
            CGRect newRect = oldRect;
            newRect.origin.y = newRect.origin.y - self.yOffset;
            self.contentView.frame = newRect;
        }

    } completion:nil];
}

-(BOOL)isShowView{
    NSArray *  arr = [self.rootWindow subviews];
    if (arr.count > 0) {
        return YES;
    }else{
        return NO;
    }
}

-(void)showCustomView:(UIView*)view{
    if(view){
         _customcontentView = view;
         _isCustom = YES;
    }
    [self show];
}
//获取按钮
-(UIButton*)getActionBtnView:(NSInteger)tagOrindex{
    id view = [self.contentView viewWithTag:tagOrindex];
    if ([view isKindOfClass:[UIButton class]]) {
        return (UIButton*)view;
    }else{
        return nil;
    }
}
-(void)hide2{
    if(self.clickBackGroundHide){
        if(self.dismissBlock){
            self.dismissBlock(nil);
        }
        [self hide];
    }
}
+(void)hideAll{
    for (UIWindow * window in [UIApplication sharedApplication].windows) {
        if (window.windowLevel == UIWindowLevelStatusBar && window.tag == AWAlertWindowsTag ) {
            window.hidden = YES;
            break;
        }
    }
}

-(void)hide{
        WEAKSELF
        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionLayoutSubviews animations:^{
            weakSelf.shodowView.alpha = 0;
            if(weakSelf.style == JFAlertViewStyleActionSheet){
                CGRect frame = self.contentView.frame;
                frame.origin.y += frame.size.height;
                [weakSelf.contentView setFrame:frame];
            }else{
                self.contentView.alpha = 0;
                CGRect newRect = weakSelf.contentView.frame;
                newRect.size.height = 40;
                newRect.size.width = 40;
                newRect.origin.x = AWSCREEN_WIDTH / 2;
                newRect.origin.y =  AWSCREENH_HEIGHT / 2;
                self.contentView.frame = newRect;
                
            }
        } completion:^(BOOL finished){
            [weakSelf removeFromSuperview];
            weakSelf.rootWindow.hidden = YES;
        }];


}
#pragma mark--监听键盘事件
-(void)keyboardWillShow:(NSNotification *)showNot{
    
    CGRect keyboardFrame= [showNot.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat upRectFrame = keyboardFrame.size.height /2;
    CGFloat duration=[showNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:duration animations:^{
        self.contentView.transform=CGAffineTransformMakeTranslation(0,-upRectFrame);
    }];
    
}

-(void)keyboardWillHide:(NSNotification *)hideNot{
    CGFloat duration=[hideNot.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        self.contentView.transform=CGAffineTransformIdentity;
    }];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark--懒加载
-(void)setCustomcontentView:(UIView *)customcontentView{
    if(customcontentView){
        _customcontentView = customcontentView;
        _isCustom = YES;
    }
}

-(UIControl*)shodowView{
    if(!_shodowView){
        _shodowView = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _shodowView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        [_shodowView addTarget:self action:@selector(hide2) forControlEvents:UIControlEventTouchUpInside];
    }
    _shodowView.frame = [UIScreen mainScreen].bounds;
    return _shodowView;
}
-(UIView*)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    }
    return _contentView;
}
- (UIWindow *)rootWindow
{
    if (_rootWindow == nil) {
        for (UIWindow * window in [UIApplication sharedApplication].windows) {
            if (window.windowLevel == UIWindowLevelStatusBar) {
                _rootWindow = window;
                break;
            }
        }
        if (!_rootWindow) {
            _rootWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
            _rootWindow.windowLevel       = UIWindowLevelStatusBar;
            _rootWindow.backgroundColor   = [UIColor clearColor];
            _rootWindow.hidden = NO;
        }
 
    }
    _rootWindow.frame = [UIScreen mainScreen].bounds;
    _rootWindow.tag = AWAlertWindowsTag;
    return _rootWindow;
}
-(NSMutableArray<JFAlertAction*>*)actionArr{
    if(!_actionArr){
        _actionArr = [[NSMutableArray alloc] init];
    }
    return _actionArr;
}
@end
